package com.iyao_app.mvp.search;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.iyao_app.R;
import com.iyao_app.adapter.SearchAdapter;
import com.iyao_app.base.BaseFragment;
import com.iyao_app.entry.HomeData;
import com.iyao_app.entry.Manor;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.entry.SearchData;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.mvp.dialog.DialogSceneActivity;
import com.iyao_app.util.Contants;
import com.iyao_app.util.DistanceUtil;
import com.iyao_app.util.MarkUtil;
import com.iyaowoqu.base_library.util.LogUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/30.
 */

public class SearchFragment extends BaseFragment implements View.OnClickListener {
    @BindView(R.id.rc_nearly_nearly_hot)
    RecyclerView mRcNearlyNearlyHot;
    Unbinder unbinder;
    private LinearLayout mLin_location;
    private SimpleDraweeView mSdv_head;
    private TextView mTv_location;
    private TextView mTv_location_detail;
    private ImageView mIv_location;
    private TextView mTv_distance;
    private TextView mTv_no_location;
    private TextView mTv_nearly_often_arrive;
    private RecyclerView mRc_nearly_often_arrive;
    private TextView mTv_nearly_hot;


    private LinearLayoutManager hotManager;
    private LinearLayoutManager oftenManager;

    private SearchAdapter oftenAdapter;
    private SearchAdapter hotAdapter;


    private String city;
    private HomeData model;
    private int type;
    private Manor manor;
    private List<SearchData> hotData;
    private List<SearchData> oftenData;
    private final int REQUEST_CODE = 0;

    @Override
    public View initRootView(LayoutInflater inflater) {
        rootView = mInflater.inflate(R.layout.fragment_search, null);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    private Intent intent = null;

    @Override
    public void setViews() {

        city = getArguments().getString(Contants.OTHER_FIELD);
        manor = (Manor) getArguments().getSerializable(Contants.THIRD_FIELD);
        type = getArguments().getInt(Contants.CLASS_TYPE, 0);
        View head = mInflater.inflate(R.layout.search_head, null);

        mLin_location = head.findViewById(R.id.lin_location);
        mSdv_head = head.findViewById(R.id.sdv_head);
        mTv_location = head.findViewById(R.id.tv_location);
        mTv_location_detail = head.findViewById(R.id.tv_location_detail);
        mIv_location = head.findViewById(R.id.iv_location);
        mTv_distance = head.findViewById(R.id.tv_distance);
        mTv_no_location = head.findViewById(R.id.tv_no_location);
        mTv_nearly_often_arrive = head.findViewById(R.id.tv_nearly_often_arrive);
        mRc_nearly_often_arrive = head.findViewById(R.id.rc_nearly_often_arrive);
        mTv_nearly_hot = head.findViewById(R.id.tv_nearly_hot);

        mLin_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


//                Manor manor = (Manor) getIntent().getSerializableExtra(Contants.THIRD_FIELD);
//                latLng = new LatLng(manor.latitude, manor.longitude);
//                id = manor.sceneId;
                if (type == 0) {
                    intent = new Intent(getActivity(), DialogSceneActivity.class);
                    intent.putExtra(Contants.THIRD_FIELD, manor);
                    startActivityForResult(intent, REQUEST_CODE);
                    getActivity().overridePendingTransition(R.anim.dialog_activity_enter, 0);
                } else {
                    intent = new Intent();
                    intent.putExtra(Contants.TYPE, manor);
                    ((SearchActivity) getActivity()).setResult(SearchActivity.RESULT_CODE, intent);
                    finish();
                }
//
            }
        });


        hotAdapter = new SearchAdapter(null);
        hotAdapter.addHeaderView(head);
        hotAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        hotAdapter.isFirstOnly(false);


        mRcNearlyNearlyHot.setAdapter(hotAdapter);
        hotManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRcNearlyNearlyHot.setLayoutManager(hotManager);

        oftenAdapter = new SearchAdapter(null);
        mRc_nearly_often_arrive.setAdapter(oftenAdapter);
        oftenManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRc_nearly_often_arrive.setLayoutManager(oftenManager);

        getData();
        oftenAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                LogUtil.e("=onItemChildClick===");
                SearchData item = (SearchData) adapter.getItem(position);
                if (item != null) {
//                    Intent intent = new Intent();
                    manor.latitude = item.getLatitude();
                    manor.longitude = item.getLongitude();
                    manor.name = item.getName();
                    manor.sceneId = item.getSceneId();
                    manor.typecode = item.getTypecode();

                    if (type == 0) {
                        intent = new Intent(getActivity(), DialogSceneActivity.class);
                        intent.putExtra(Contants.THIRD_FIELD, manor);
                        startActivityForResult(intent, REQUEST_CODE);
                        getActivity().overridePendingTransition(R.anim.dialog_activity_enter, 0);
                    } else {
                        intent = new Intent();
                        intent.putExtra(Contants.TYPE, manor);
                        ((SearchActivity) getActivity()).setResult(SearchActivity.RESULT_CODE, intent);
                        finish();
                    }

                }
            }
        });
        hotAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                LogUtil.e("=onItemChildClick===");
                SearchData item = (SearchData) hotAdapter.getItem(position);
                if (item != null) {
                    LogUtil.e("=item!=null===");
//                    Intent intent = new Intent();
                    manor.latitude = item.getLatitude();
                    manor.longitude = item.getLongitude();
                    manor.name = item.getName();
                    manor.sceneId = item.getSceneId();
                    manor.typecode = item.getTypecode();
                    if (type == 0) {
                        intent = new Intent(getActivity(), DialogSceneActivity.class);
                        intent.putExtra(Contants.THIRD_FIELD, manor);
                        startActivityForResult(intent, REQUEST_CODE);
                        getActivity().overridePendingTransition(R.anim.dialog_activity_enter, 0);
                    } else {
                        intent = new Intent();
                        intent.putExtra(Contants.TYPE, manor);
                        ((SearchActivity) getActivity()).setResult(SearchActivity.RESULT_CODE, intent);
                        finish();
                    }

//                    intent.putExtra(Contants.TYPE, manor);
//                    ((SearchActivity) getActivity()).setResult(SearchActivity.RESULT_CODE, intent);
//                    finish();

                }
            }
        });
    }

    @Override
    public void initData() {
        if (manor != null) {
            mLin_location.setVisibility(View.VISIBLE);
            mTv_no_location.setVisibility(View.GONE);
            mSdv_head.setImageResource(MarkUtil.getId(manor.typecode));
            mTv_location.setText(manor.name);
            mTv_distance.setText(DistanceUtil.getDitance(manor.distance));
            mTv_location_detail.setText(manor.address);
        } else {
            mLin_location.setVisibility(View.GONE);
            mTv_no_location.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void getData() {
        if (manor == null) return;
        NetUtils.getInstance().hotScene(manor.longitude, manor.latitude, Contants.RADIUS, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model == null) {//没有数据
                        mTv_nearly_hot.setVisibility(View.GONE);
                    } else {
                        mTv_nearly_hot.setVisibility(View.VISIBLE);
                        hotData = (List<SearchData>) model.getModelList();
                        hotAdapter.addData(hotData);
                    }
                }

            }

            @Override
            public void onFail(String message, int code) {

            }
        }, SearchData.class);
        NetUtils.getInstance().oftenArrive(manor.longitude, manor.latitude, Contants.RADIUS, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model == null) {//没有数据
                        mRc_nearly_often_arrive.setVisibility(View.GONE);
                        mTv_nearly_often_arrive.setVisibility(View.GONE);
                    } else {
                        mRc_nearly_often_arrive.setVisibility(View.VISIBLE);
                        mTv_nearly_often_arrive.setVisibility(View.VISIBLE);
                        oftenData = (List<SearchData>) model.getModelList();
                        oftenAdapter.addData(oftenData);
                    }
                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, SearchData.class);
    }

    @Override
    public void onClick(View v) {

    }
}
